********************************************************************************
** 	TITLE: d2_reduced_form_binscatter.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Figure II: Reduced-Form Effects of Court Delays by Booking Month

********************************************************************************
		set sortseed 13
		use "$output_data/estimate.dta", clear

**Binned scatter	
		foreach v in num_maj_weekly return_3m {
			
				//stay ignite_0 for misconduct
			if "`v'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
				//stay ignite_0 for Recidivism
			if "`v'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
				
				//Conditions for Sample
				if "`v'" == "return_3m"{
					local cond if return_3m>=0
				}
				if ("`v'" == "D_maj_weekly"){
					local cond ""
				}

//RF Regression				
				reghdfe `v' i.ym_booked#D_remove_any i.ym_booked $auxiliary_control `cond' ,  a($design_control) 

				mat define R=J(200,2,.)
				forvalues j=662/755{
					cap mat R[`j'-661,1] = _b[`j'.ym_booked#1.D_remove_any]
					cap mat R[`j'-661,2] = `j'
				}
				
				preserve 
				keep if e(sample)==1
				collapse (count) bin_size = booking_date, by(ym_booked)
				ren ym_booked ym
				
				tempfile size
				save `size'
				
				restore
				
				preserve
				svmat R
				ren (R1 R2 ) (c ym)
				keep c ym
				keep if !mi(c)
				
				merge 1:1 ym using `size', nogen
				
				lpoly c ym if ym<728 [aw=bin_size], nograph  generate(g1 s1) se(se1) bwidth(14) d(1)
				lpoly c ym if ym>=728 [aw=bin_size] , nograph  generate(g2 s2) se(se2) bwidth(14) d(1)


				forvalues j=1/2{
					gen ul`j' = s`j'+1.96*se`j'
					gen ll`j' = s`j'-1.96*se`j'
				}
				local xl  xline(728, lc(gs8) lp(solid) lwidth(*1.3))  yline(0,lc(black))
				local xlab xlab(672(18)744 , format(%tm) labsize(*.9))
				
				if "`v'" == "num_maj_weekly"{
					local title "Weekly major misconduct"
					local ylab ylab(-.15(.075).15,labsize(*.9))
					local text text(.125 735.9 "Start of IGNITE", size(*.85))
					local savet "figure2a_bscatter_miscond"
					
				}
				if "`v'" == "return_3m"{
					local title "Three-month recidivism"
					local ylab ylab(-.15(.075).15,labsize(*.9))
					local text text(.125 735.9 "Start of IGNITE", size(*.85))
					local savet "figure2b_bscatter_recid"

					}
							
				
//Bin Scatter Plots					
				tw 	(scatter  c ym if ym<728 , sort mc(%80) mc(gs0) msymbol(Dh)) ///msymbol(Dh Oh)
					(scatter  c ym if ym>=728, sort mc(%80) mc(gs8) msymbol(Oh)) ///
					(rarea ul1 ll1 g1 , sort fc(gs0%30) lc(%0)) ///
					(rarea ul2 ll2 g2 , sort fc(gs8%30) lc(%0) ) ///
					(line s1 g1 , sort lc(gs0) lwidth(medium)) ///
					(line s2 g2 , sort lc(gs8) lwidth(medium)) ///
					, ///
					legend(off) `xl' ///735
					xtitle("Booking month") ytitle("`title'") ///
					`ylab' `xlab' `text' ysize(4) xsize(5.5)
				graph export "$figures/`savet'.pdf", replace
				graph export "$figures/`savet'.tif",  width(1650) height(1200) replace //300 dpi
				restore
		}